#!/bin/bash
# 配置环境
function MySystem(){
uname -o
}
Packages(){
WhichSys=`MySystem`
Linux="GNU/Linux"
if [[ "$WhichSys" != "$Linux" ]] ; then
echo -e "\n\n\n\n\n\n\n\t\t您需要一个Linux系统\n\n\n\n\n\n\n\n\n"
exit
fi
if grep -Eqii "Arch Linux" /etc/issue || grep -Eq "Arch Linux" /etc/*-release; then
sudo pacman -S wget python python-protobuf android-tools
else
sudo apt-get install wget python python-protobuf android-tools-fastboot
fi
}
# 检查设备
CheckDevice(){
fastboot $* getvar product 2>&1 | grep "^product: *alioth"
if [ $? -ne 0  ] ; then echo -e "\n\n\n\n\n\n\n\n\t\t请插入正确的设备\n\n\n\n\n\n\n\n"; exit 1; fi
}
# 准备刷机包
PrePare(){
git clone https://gitee.com/baizhi958216/lineage-os_scripts.git --depth=1
if [ ! -d lineage-os_scripts ];then
  git clone https://github.com/LineageOS/scripts.git lineage-os_scripts --depth=1
fi
wget -O alioth.zip https://hugeota.d.miui.com/21.11.9/miui_ALIOTH_21.11.9_62dee71477_12.0.zip
unzip alioth.zip payload.bin
python lineage-os_scripts/update-payload-extractor/extract.py payload.bin --output_dir ./out
}
# 刷机
Flash(){
cd ./out
FastbootFiles=(abl aop bluetooth boot cmnlib cmnlib64 devcfg dsp dtbo featenabler hyp imagefv keymaster modem qupfw tz uefisecapp vbmeta vbmeta_system vendor_boot xbl xbl_config)
FastbootdFiles=(system system_ext vendor product odm)
SystemSize=`wc -c < ${FastbootdFiles[0]}.img`
SystemExtSize=`wc -c < ${FastbootdFiles[1]}.img`
VendorSize=`wc -c < ${FastbootdFiles[2]}.img`
ProductSize=`wc -c < ${FastbootdFiles[3]}.img`
OdmSize=`wc -c < ${FastbootdFiles[4]}.img`
echo -e "\n\n\n\n\n\n\n\n\t\t5秒后开始刷入，请勿挪动数据线或设备\n\n\n\n\n\n\n\n"
sleep 5
for ((i=0; i<${#FastbootFiles[*]}; i++))
do
fastboot flash ${FastbootFiles[i]}_ab ${FastbootFiles[i]}.img
done
echo -e "\n\n\n\n\n\n\n\n\t\t重启进入fastbootd\n\n\n\n\n\n\n\n"
fastboot reboot fastboot
for ((i=0; i<${#FastbootdFiles[*]}; i++))
do
fastboot delete-logical-partition ${FastbootdFiles[i]}_a
fastboot delete-logical-partition ${FastbootdFiles[i]}_b
done
fastboot create-logical-partition ${FastbootdFiles[0]}_a $SystemSize
echo -e "\n\n\n\t\t\t创建了一个大小为$SystemSize的逻辑${FastbootdFiles[0]}分区a\t\t\t\n\n\n"
fastboot create-logical-partition ${FastbootdFiles[1]}_a $SystemExtSize
echo -e "\n\n\n\t\t\t创建了一个大小为$SystemExtSize的逻辑${FastbootdFiles[1]}分区a\t\t\t\n\n\n"
fastboot create-logical-partition ${FastbootdFiles[2]}_a $VendorSize
echo -e "\n\n\n\t\t\t创建了一个大小为$VendorSize的逻辑${FastbootdFiles[2]}分区a\t\t\t\n\n\n"
fastboot create-logical-partition ${FastbootdFiles[3]}_a $ProductSize
echo -e "\n\n\n\t\t\t创建了一个大小为$ProductSize的逻辑${FastbootdFiles[3]}分区a\t\t\t\n\n\n"
fastboot create-logical-partition ${FastbootdFiles[4]}_a $OdmSize
echo -e "\n\n\n\t\t\t创建了一个大小为$OdmSize的逻辑${FastbootdFiles[4]}分区a\t\t\t\n\n\n"
for ((i=0; i<${#FastbootdFiles[*]}; i++))
do
fastboot flash ${FastbootdFiles[i]}_a ${FastbootdFiles[i]}.img
done
slot=`fastboot $* getvar current-slot 2>&1 | grep "^current-slot: *"`
if [[ "$slot" != "current-slot: a" ]] ; then
fastboot set_active a
fi
fastboot reboot
}
Packages
CheckDevice
PrePare
Flash
